package cn.edu.answer.controller;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.answer.po.Student;
import cn.edu.answer.service.StudentService;
import cn.edu.answer.utils.Page;




/**
 * 学生管理控制器类
 */
@Controller
public class StudentController {
	// 依赖注入
	@Autowired
	private StudentService studentService;


	/**
	 *  学生列表
	 */
	@RequestMapping(value = "/student/list.action")
	public String list(@RequestParam(defaultValue="1")Integer page,
			@RequestParam(defaultValue="10")Integer rows, Model model) {
		// 所有学生列表
		Page<Student> students = studentService.findStudentList(page,rows);
		model.addAttribute("page", students);	
		System.out.println("===="+students.getSize());
		System.out.println("===="+students);
		return "student_list";
	}
	
	/**
	 * 添加学生
	 */
	@RequestMapping("/student/createStudent.action")
	@ResponseBody
	public String cateStudent(Student student,HttpSession session) {

	    // 执行Service层中的创建方法，返回的是受影响的行数
	    int rows = studentService.createStudent(student);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
	}
	/**
	 * 删除学生信息
	 */
	@RequestMapping("/student/deleteStudent.action")
	@ResponseBody
	public String studentDelete(String sno) {
	    int rows = studentService.deleteStudent(sno);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	

	@RequestMapping(value = "/student/getlist.action")
	public String getlist(@RequestParam(value="studentName")String name,Model model) {
		System.out.print(name);
		// 所有寝室列表
		
		  List<Student> student = studentService.getStudentList(name);
		  model.addAttribute("name", student);
		  System.out.print(student);
		return "student_list2";
	}
	
	
}
